package context.dao;

import context.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class UserinformationRepository {
    private final String SELECT_BY_ID = "select * from userinformation where userid=?";

    private final String INSERT_USER = "insert into userinformation(userid,phoneNumber,gender,age,work,qq,dataVisibility,img) values(?,?,?,?,?,?,?,?)";

    private JdbcTemplate jdbcTemplate;

    @Autowired          //自动装配
    public UserinformationRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public User findUserByID(long id) {

        try {
            return jdbcTemplate.queryForObject(SELECT_BY_ID, this::mapRow, id);
        } catch (EmptyResultDataAccessException e) {
            System.out.println("select user null");
            return null;
        }

    }


    private User mapRow(ResultSet resultSet, int i) throws SQLException {
        User result = new User();
        result.setId(resultSet.getLong("userid"));
        result.setPhoneNumber(resultSet.getString("phoneNumber"));
        result.setGender(resultSet.getString("gender"));
        result.setAge(resultSet.getInt("age"));
        result.setWork(resultSet.getString("work"));
        result.setQq(resultSet.getString("qq"));
        result.setDataVisibility(resultSet.getInt("dataVisibility"));
        result.setUserimg(resultSet.getString("img"));
        return result;
    }

    public int insertUserInformation(final int userid, final String phoneNumber, final String gender, final int age, final String work, final String qq, final int dataVisibility, final String img) {

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(
                new PreparedStatementCreator() {
                    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                        PreparedStatement ps = jdbcTemplate.getDataSource()
                                .getConnection().prepareStatement(INSERT_USER, new String[]{"userid", "phoneNumber", "gender", "age", "work", "qq", "dataVisibility", "img"});
                        ps.setInt(1, userid);
                        ps.setString(2, phoneNumber);
                        ps.setString(3, gender);
                        ps.setInt(4, age);
                        ps.setString(5, work);
                        ps.setString(6, qq);
                        ps.setInt(7, dataVisibility);
                        ps.setString(8, img);
                        return ps;
                    }
                }, keyHolder);
        System.out.println("自动插入id============================" + keyHolder.getKey().intValue());
        return keyHolder.getKey().intValue();
    }

    //改(除头像)
    private static final String sql = "update userinformation set gender =?,age=?,work=?,qq=?,dataVisibility=? where userid = ? ";

    public void updateUserInformation(int userid, int age, int dataVisibility, String gender, String work, String qq) {
        Object args[] = new Object[]{userid, age, dataVisibility, gender, work, qq};
        int temp = jdbcTemplate.update(sql, args);
        if (temp > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }

    }

    //改头像
    private static final String sql1 = "update userinformation set img=? where userid = ? ";

    public void updateUserImg(int userid, String img) {
        Object args[] = new Object[]{userid, img};
        int temp = jdbcTemplate.update(sql1, args);
        if (temp > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }

    }

    private String selectUserimg = "select img from userinformation where userid=?";

    public String getUserimg(long userid) {
        try {
            return jdbcTemplate.queryForObject(selectUserimg, new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet resultSet, int i) throws SQLException {
                    return resultSet.getString(1);
                }
            }, userid);
        } catch (EmptyResultDataAccessException e) {
            return "";
        }

    }
}